<?php
/**
 * Created by PhpStorm.
 * User: xuechunxuan
 * Date: 2018/10/18
 * Time: 15:23
 */

namespace app\index\controller;

use app\index\model\Consume;
use think\Request;

class Home extends Session
{

    private function getSellerConsume()
    {
        return new Consume();
    }


    /*
     * 海报大厅
     * @param bannerPath string 海报路径
     * @param bannerPrice int  海报单价
     * @param bannerUV  int 海报热度
     * @param addTime int 添加时间戳
     * */
    public function home(Request $request)
    {
        $page = $request->param("page") ? $request->param("page") : 0;
        $consumeArr = $this->getSellerConsume()->mySelectByKVPage(['is_show'=>1,"seller_id"=>0], $page, 9);
        $this->getUser()->edit(['id'=>$this->userInfo['id'],'first_login'=>1]);//首次登陆修改为1
        if ($request->isGet()) {
            $this->assign("showQrcode",$this->getUserFocusApp());
            $this->assign("consumeArr",$consumeArr);
            return $this->fetch();
        } else if ($request->isPost()) {


            return json($consumeArr, 200);
        }
    }

    /*
     * 判断用户是否关注官方公众号
     * */
    public function getUserFocusApp(){
        $url=config("API_HOST").config("GET_USER_FOCUS_1").$this->userInfo['openid'].config("GET_USER_FOCUS_2").config("PAY_APP_ID");
        $focusInfo=json_decode($this->http_request($url),true);
        return $focusInfo['isFollowed']===true ? 1 : 0;
    }

    /*
     * 海报详情
     * */
    public function getConsumeById(Request $request)
    {

        $data = $request->param();
        if ($request->isPost()) {
            if($this->userInfo['is_spread']==1){
                $data=['code'=>404,'msg'=>'您是推广用户无法生成海报'];
                return json_encode($data);
            }
            if (array_key_exists("id", $data)&&is_numeric($data['id'])) {
                $consumeInfo = $this->getSellerConsume()->findById($data['id']);//查询对应图片信息

                $data = [
                    "shareUserOpenid" => $this->userInfo['openid'],
                    "srcAppid" => "wx97e0e09eacd7657a",
                    "redirectAppid" => $consumeInfo['app_id'],
                    "bizId" => "kBuqTrYE",
                    "targetFollowerCount" => "800000",
                    "productId" => "100000000".$data['id']
                ];

                //请求图片info
                $imgInfo=json_decode($this->http_request(config("API_HOST").config("GET_SHARE_IMG"),false,"post",json_encode($data)),true);
                if(is_array($imgInfo)){
                 $url=config("API_HOST").config("GET_SHARE_CONSUME_1");
                 $data=[
                      "posterId"=>$consumeInfo['qr_code_id'],
                     "shareId"=>$imgInfo['id']
                 ];
                 $consumeImgInfo=json_decode($this->http_request($url,false,"post",json_encode($data)),true);
                 if(array_key_exists("url",$consumeImgInfo)) {
                     $data = [
                         'user_id' => \think\Session::get("uid"),
                         'img_path' => $consumeImgInfo['url'],
                         'consume_id'=>$consumeInfo['id'],
                         'pie_id'=>$imgInfo['id']
                     ];
                     $where=[
                         'user_id'=>\think\Session::get("uid"),
                         'consume_id'=>$consumeInfo['id'],
                     ];
                     if(!$this->getShareHistory()->findByKV($where)) {
                         $this->getShareHistory()->add($data);//保存对应用户分享图片记录到数据库
                     }
                 }
                 $consumeImgInfo['slogan']=$consumeInfo['slogan'];//广告语
                return json_encode($consumeImgInfo);
               }
            }
        }
    }
}